﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GestionePolveri2.DAL;
using GestionePolveriLib.BL.Reports;
using FrameworkLib.Facades;

namespace GestionePolveriLib.Facades.Reports
{
    public class Report523Facade : FacadeBase<GestionePolveriEntities>
    {
        Filter523 m_filter = null;

        public Report523Facade()
        {

        }

        public Report523Facade(Filter523 filter)
        {
            m_filter = filter;
        }

        public List<Report523> GetReport523Data()
        {
            List<Report523> retValue = null;

            if (m_filter.Lotto == -1)
                retValue = GetReport523Data1();
            else
                retValue = GetReport523Data2();

            return retValue;
        }

        private List<Report523> GetReport523Data1()
        {
            List<Report523> retValue = null;

            GestionePolveriEntities context = this.GetContext();
            var obj = from x in context.Movimenti
                      where ((x.DataMovimento >= m_filter.PeriodFrom) && (x.DataMovimento <= m_filter.PeriodTo))
                            && x.CausaliMagazzino.CodCausale == "S"
                      select new Report523
                      {
                          DataPrelievo = x.DataMovimento,
                          Impianto = x.Impianti.Impianto,
                          Operatore = x.aspnet_Users.UserName,
                          Quantita = x.Quantita
                      };

            if (obj.Any())
            {
                retValue = obj.ToList();
            }

            return retValue;
        }

        private List<Report523> GetReport523Data2()
        {
            List<Report523> retValue = null;

            GestionePolveriEntities context = this.GetContext();
            var obj = from x in context.Movimenti
                      where ((x.DataMovimento >= m_filter.PeriodFrom) && (x.DataMovimento <= m_filter.PeriodTo))
                            && x.IDLotto == m_filter.Lotto
                            && x.CausaliMagazzino.CodCausale == "S"
                      select new Report523
                      {
                          DataPrelievo = x.DataMovimento,
                          Impianto = x.Impianti.Impianto,
                          Operatore = x.aspnet_Users.UserName,
                          Quantita = x.Quantita
                      };

            if (obj.Any())
            {
                retValue = obj.ToList();
            }

            return retValue;
        }
    }
}
